package com.mky.cloud.controller;

import com.mky.cloud.entity.Dept;
import com.mky.cloud.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class DeptController {

    @Autowired
    private DeptService deptService;
    @Resource   //服务发现
    private DiscoveryClient client;

    @RequestMapping(value = "/dept/add",method = RequestMethod.POST)
//    @RequestMapping("/dept/add")
    public boolean add(@RequestBody Dept dept){
        return deptService.add(dept);
    }

    @RequestMapping(value = "/dept/get/{id}",method = RequestMethod.GET)
//    @RequestMapping("/dept/get/{id}")
    public Dept get(@PathVariable("id") Long id){
        return deptService.get(id);
    }

    @RequestMapping(value = "/dept/list",method = RequestMethod.GET)
//    @RequestMapping("/dept/list")
    public List<Dept> list(){
        return deptService.list();
    }


    //服务发现
    @RequestMapping(value = "/dept/discovery",method = RequestMethod.GET)
    public Object discovery(){
        //盘点Eureka内的微服务
        List<String> list = client.getServices();
        System.out.println("@@@@@@@@@@" + list);
        //在Eureka内的微服务找到一个部门的微服务
        List<ServiceInstance> srvList = client.getInstances("MICROSERVICECLOUD-DEPT");
        //打印出部门微服务的信息
        for (ServiceInstance element : srvList){
            System.out.println(element.getServiceId() + "\t" +element.getPort() +
                    "\t" + element.getUri());
        }
        return this.client;
    }

}
